package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ItemUser;
import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserCredit;
import cn.wolfcode.trip.base.mapper.OrderMapper;
import cn.wolfcode.trip.base.query.OrderQueryObject;
import cn.wolfcode.trip.base.service.IItemService;
import cn.wolfcode.trip.base.service.IItemUserService;
import cn.wolfcode.trip.base.service.IOrderService;
import cn.wolfcode.trip.base.service.IUserCreditService;
import cn.wolfcode.trip.base.util.AccountMessage;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IUserCreditService creditService;

    @Autowired
    private IItemService itemService;

    @Autowired
    private IItemUserService itemUserService;


    @Override
    public void delete(Long id) {
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }

    @Override
    public void update(Order order) {
        orderMapper.updateByPrimaryKey(order);
    }

    @Override
    public AccountMessage saveOrder(Order order) {
        AccountMessage result = new AccountMessage();
        // 获取商品的价格
        if (order.getItem() != null) {
            Long itemId = order.getItem().getId();
            int price = itemService.getItemPriceByItemId(itemId);
            // 保存商品的价格
            order.setPrice(price);
        } else {
            result.setSuccess(false);
            result.mark("系统错误,请稍后再试");
            return result;
        }

        // 核对用户账户上积分是否足够
        User user = order.getUser();
        if (user != null) {
            UserCredit userCredit = creditService.getCreditsByUserId(user.getId());
            // 用户当前积分数
            Integer currentCredit = userCredit.getCurrentCredit();
            // 检查用户积分是否足够支付
            if(currentCredit < order.getCount()*order.getPrice()){
                result.setSuccess(false);
                result.setMsg("您的积分不足,支付失败");
                return result;
            }

        } else {
            result.setSuccess(false);
            result.mark("系统错误,请稍后再试");
            return result;
        }

        // 用户积分足够,扣除积分
        Long id = order.getUser().getId();
        int price = order.getCount() * order.getPrice();
        creditService.decreaseCreditByUserId(id, price);
        /*try {
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.mark("系统错误,请稍后再试");
            return  result;
        }*/
        // 支付成功,保存订单记录
        order.setState(Order.STATE_WAITING); // 未发货
        Date inputTime = new Date(); // 订单生成时间
        order.setInputTime(inputTime); // 订单生成时间
        if (order.getMethod() == null) {
            order.setMethod(Order.POST_FREE); // 默认包邮
        }
        // 订单号
        order.setOrdernum(inputTime.getTime() + ""); // 随机订单号
        orderMapper.insert(order);

        // 支付成功,删除原来购物车中的记录
        ItemUser shopcar = order.getShopcar();
        if (shopcar != null) {
            itemUserService.deleteByPrimaryKey(shopcar.getId());
        }else {
            throw new  RuntimeException("系统错误,请稍后再试");
        }

        result.setSuccess(true);
        result.mark("支付成功!");
        return result;
    }

    @Override
    public List<Order> selectOrdersByUserId(OrderQueryObject qo) {
        return orderMapper.selectOrdersByUserId(qo);
    }


    @Override
    public Order get(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

}
